/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.course.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.course.param.*;
import vip.xiaonuo.biz.modular.klass.entity.EduKlass;
import vip.xiaonuo.biz.modular.klass.service.EduKlassService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.biz.modular.course.entity.EduCourse;
import vip.xiaonuo.biz.modular.course.service.EduCourseService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 课程控制器
 *
 * @author ahui
 * @date  2023/05/13 21:20
 */
@Api(tags = "课程控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class EduCourseController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduKlassService eduKlassService;

    /**
     * 获取课程分页
     *
     * @author ahui
     * @date  2023/05/13 21:20
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取课程分页")
    @SaCheckPermission("/biz/course/page")
    @GetMapping("/biz/course/page")
    public CommonResult<Page<EduCourse>> page(EduCoursePageParam eduCoursePageParam) {
        return CommonResult.data(eduCourseService.page(eduCoursePageParam));
    }

    /**
     * 添加课程
     *
     * @author ahui
     * @date  2023/05/13 21:20
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加课程")
    @CommonLog("添加课程")
    @SaCheckPermission("/biz/course/add")
    @PostMapping("/biz/course/add")
    public CommonResult<String> add(@RequestBody @Valid EduCourseAddParam eduCourseAddParam) {
        if (ObjectUtil.isNotNull(eduCourseService.getByName(eduCourseAddParam.getName()))) {
            throw new CommonException("课程已存在", eduCourseAddParam.getName());
        }
        eduCourseService.add(eduCourseAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑课程
     *
     * @author ahui
     * @date  2023/05/13 21:20
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑课程")
    @CommonLog("编辑课程")
    @SaCheckPermission("/biz/course/edit")
    @PostMapping("/biz/course/edit")
    public CommonResult<String> edit(@RequestBody @Valid EduCourseEditParam eduCourseEditParam) {
        eduCourseService.edit(eduCourseEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除课程
     *
     * @author ahui
     * @date  2023/05/13 21:20
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除课程")
    @CommonLog("删除课程")
    @SaCheckPermission("/biz/course/delete")
    @PostMapping("/biz/course/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<EduCourseIdParam> eduCourseIdParamList) {
        eduCourseService.delete(eduCourseIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取课程详情
     *
     * @author ahui
     * @date  2023/05/13 21:20
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取课程详情")
    @SaCheckPermission("/biz/course/detail")
    @GetMapping("/biz/course/detail")
    public CommonResult<EduCourse> detail(@Valid EduCourseIdParam eduCourseIdParam) {
        return CommonResult.data(eduCourseService.detail(eduCourseIdParam));
    }

    /**
     * 计划导入到课程
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("导入到配课")
    @CommonLog("导入到配课")
    @SaCheckPermission("/biz/course/exportCourse")
    @GetMapping("/biz/course/exportCourse")
    public CommonResult<String> exportCourse(EduCourseExportParam eduCourseExportParam) {
        eduCourseService.assign(eduCourseExportParam);
        return CommonResult.ok();
    }

    /**
     * 一键配课
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("一键配课")
    @CommonLog("一键配课")
//    @SaCheckPermission("/biz/course/smartExport")
    @GetMapping("/biz/course/smartExport")
    public CommonResult<String> smartExport(EduCourseExportParam eduCourseExportParam) {
        System.out.println(eduCourseExportParam.getCourseIds());
        eduCourseService.assign(eduCourseExportParam);
        return CommonResult.ok();
    }

    /**
     * 查询所有班级
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("查询所有班级")
    @CommonLog("查询所有班级")
    @SaCheckPermission("/biz/course/eduKlassList")
    @GetMapping("/biz/course/eduKlassList")
    public CommonResult<List<EduKlass>> eduKlassList() {
        List<EduKlass> list = eduKlassService.list();
        return CommonResult.data(list);
    }

    /**
     * 直接开课
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("直接开课")
    @CommonLog("直接开课")
    @SaCheckPermission("/biz/course/offer")
    @GetMapping("/biz/course/offer")
    public CommonResult<String> offer(EduCourseOfferParam eduCourseOfferParam) {
        eduCourseService.offer(eduCourseOfferParam);
        return CommonResult.ok();
    }

}
